{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "tags": [
     "remove-cell"
    ]
   },
   "outputs": [],
   "source": [
    "import set_env"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python 端加载 C++ 端动态库\n",
    "\n",
    "可以使用 {mod}`ctypes` 加载动态库："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ctypes\n",
    "\n",
    "# 作为全局加载，使全局外部符号对其他 dll 可见。\n",
    "_LIB = ctypes.CDLL(\"../cpp/sym_add/outputs/libs/libtvm_ext.so\", ctypes.RTLD_GLOBAL)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "加载失败，是由于 `libtvm_ext.so` 是在 `libtvm.so` 基础上拓展的，故而需要先提前加载 `libtvm.so`，或者直接 `import tvm`："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tvm\n",
    "import ctypes\n",
    "\n",
    "# 作为全局加载，使全局外部符号对其他 dll 可见。\n",
    "_LIB = ctypes.CDLL(\"../cpp/sym_add/outputs/libs/libtvm_ext.so\", ctypes.RTLD_GLOBAL)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "加载动态库，也可以直接使用 {func}`~tvm_book.tvm_ext.libinfo.load_lib`："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "tags": [
     "remove-cell"
    ]
   },
   "outputs": [],
   "source": [
    "# 需要重启 jupyter kernel \n",
    "import set_env"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tvm_book.tvm_ext.libinfo import load_lib\n",
    "\n",
    "_LIB_EXT, _LIB_EXT_NAME = load_lib(name=\"libtvm_ext.so\", search_path=[\"../cpp/sym_add/outputs/libs\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "回调 C++ 函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tvm\n",
    "sym_add = tvm.get_global_func(\"tvm_ext.testing.sym_add\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "测试："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x + y\n"
     ]
    }
   ],
   "source": [
    "from tvm import te\n",
    "x = te.var(\"x\")\n",
    "y = te.var(\"y\")\n",
    "z = sym_add(x, y)\n",
    "assert z.a == x and z.b == y\n",
    "print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
